package com.xinke.module.system.controller.admin.user;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.user.*;
import com.xinke.module.system.model.entity.user.DeptDO;
import com.xinke.module.system.service.user.DeptService;

@Tag(name = "管理后台 - 部门")
@RestController
@RequestMapping("/system/dept")
@Validated
public class DeptController {

    @Resource
    private DeptService deptService;

    @GetMapping("/list")
    @Operation(summary = "获得部门列表")
    @PreAuthorize("@ss.hasPermission('system:dept:query')")
    public Result<List<DeptRespVO>> getList(@Valid DeptPageReqVO pageReqVO) {
        List<DeptRespVO> deptList = deptService.getAllDepts();
        return success(deptList);
    }

    @PostMapping("/create")
    @Operation(summary = "创建部门")
    @PreAuthorize("@ss.hasPermission('system:dept:create')")
    public Result<Long> createDept(@Valid @RequestBody DeptSaveReqVO createReqVO) {
        return success(deptService.createDept(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新部门")
    @PreAuthorize("@ss.hasPermission('system:dept:update')")
    public Result<Boolean> updateDept(@Valid @RequestBody DeptSaveReqVO updateReqVO) {
        deptService.updateDept(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete/{deptId}")
    @Operation(summary = "删除部门")
    @Parameter(name = "deptId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:dept:delete')")
    public Result<Boolean> deleteDept(@PathVariable("deptId") Long deptId) {
        deptService.deleteDept(deptId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得部门")
    @Parameter(name = "deptId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:dept:query')")
    public Result<DeptRespVO> getDept(@RequestParam("deptId") Long deptId) {
        DeptDO dept = deptService.getDept(deptId);
        return success(BeanUtils.toBean(dept, DeptRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得部门分页")
    @PreAuthorize("@ss.hasPermission('system:dept:query')")
    public Result<PageResult<DeptRespVO>> getDeptPage(@Valid DeptPageReqVO pageReqVO) {
        PageResult<DeptDO> pageResult = deptService.getDeptPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeptRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出部门 Excel")
    @PreAuthorize("@ss.hasPermission('system:dept:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeptExcel(@Valid DeptPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeptDO> list = deptService.getDeptPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "部门.xls", "数据", DeptRespVO.class,
                        BeanUtils.toBean(list, DeptRespVO.class));
    }

}